package com.sabm.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sabm.dao.ContactDAO;
import com.sabm.dao.ContactDAOImpl;
import com.sabm.dao.UserDAO;
import com.sabm.dao.UserDAOImpl;
import com.sabm.model.Account;
import com.sabm.model.Address;
import com.sabm.model.Contact;
import com.sabm.model.Email;
import com.sabm.model.Phone;
import com.sabm.model.Role;
@Service
public class DatabaseService {
	@Autowired
	private ContactDAO contactDAO;
	@Autowired
	private UserDAO userDAO;
	// add new contact
	@Transactional
	public void addContact(Contact contact) {
		contactDAO.save(contact);
		for(Address address : contact.getAddresses()){
    		address.setContact(contact);
    		contactDAO.saveOrUpdateAddress(address);
    	}
    	for(Email email : contact.getEmails()){
    		email.setContact(contact);
    		contactDAO.saveOrUpdateEmail(email);
    	}
    	for(Phone phone : contact.getPhones()){
    		phone.setContact(contact);
    		contactDAO.saveOrUpdatePhone(phone);
    	}
    }
	// delete contact
	@Transactional
    public void deleteContact(Contact contact) {
    	contactDAO.delete(contact);
    }
    // update contact
	@Transactional
    public void updateContact(Contact contact) {
    	contactDAO.update(contact);
    	for(Address address : contact.getAddresses()){
    		address.setContact(contact);
    		contactDAO.saveOrUpdateAddress(address);
    	}
    	for(Email email : contact.getEmails()){
    		email.setContact(contact);
    		contactDAO.saveOrUpdateEmail(email);
    	}
    	for(Phone phone : contact.getPhones()){
    		phone.setContact(contact);
    		contactDAO.saveOrUpdatePhone(phone);
    	}
    }
    ///
    ///
	@Transactional
    public void addAccount(Account account) {
		userDAO.save(account);
		for(Role role : account.getRoles()){
			role.setAccount(account);
			userDAO.saveOrUpdateRole(role);
		}
    }
    // delete account
	@Transactional
    public void deleteAccount(Account account) {
    	userDAO.delete(account);
    }
    // update account
	@Transactional
    public void updateAccount(Account account) {
    	userDAO.update(account);
    }
	@Transactional
    public Account getAccount(int id){
    	return userDAO.get(id);
    }
	@Transactional
	public Account getAccountByUsername(String username){
		return userDAO.get(username);
	}
	@Transactional
	public Contact getContactById(int idContact){
		return contactDAO.getContactById(idContact);
	}
	@Transactional
	public List<Contact> getContactsByAccount(int idAccount){
		return contactDAO.getListContacts(idAccount);
	}
	@Transactional
	public List<Contact> getContactsByAccount(int idAccount, int start, int length){
		return contactDAO.getListContacts(idAccount, start, length);
	}
	// count number contacts of current user
	@Transactional
	public int getTotalContactsByAccount(int idAccount){
		return contactDAO.getTotalContacts(idAccount);
	}
	@Transactional
	public List<Account> getListAccounts(){
		return userDAO.getListAccount();
	}
	@Transactional
	public boolean checkUsername(String username){
		return userDAO.checkUsername(username);
	}
	
}
